package com.shisfish.library.Controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;

import com.shisfish.library.Impl.BarcodeImpl;
import com.shisfish.library.Impl.BookImpl;
import com.shisfish.library.Impl.BookMessageImpl;
import com.shisfish.library.Impl.BookSingleImpl;
import com.shisfish.library.Model.Barcode;
import com.shisfish.library.Model.Book;
import com.shisfish.library.Model.BookMessage;
import com.shisfish.library.Model.BookSingle;
import com.shisfish.library.Prere.Login;
import com.shisfish.library.Utils.Global.GlobalVariable;
import com.shisfish.library.Utils.Global.toJsonUtils;
import com.shisfish.library.Utils.PageModel.Page;
import com.shisfish.library.Utils.PageModel.PageUtils;

@Path("bookSingle")
public class BookSingleController extends GlobalVariable {

	private BookSingleImpl bookSingleImpl = new BookSingleImpl();
	
	private BookMessageImpl bookMessageImpl = new BookMessageImpl();
	
	private BookImpl bookImpl = new BookImpl();
	
	private BarcodeImpl barcodeImpl = new BarcodeImpl();
	
	/**
	 * @Author shisfish
	 * @Date 2016年1月5日 下午4:19:23
	 * @Descrition : 添加单本书信息
	 * @param idadmin
	 * @param barCode
	 * @param isbn
	 * @param position
	 * @param request
	 * @return
	 */
	@POST
	@Path("insertASingleBook/{idadmin}")
	@Produces("application/json; charset=utf-8")
	public String insertASingleBook(@PathParam("idadmin") String idadmin, @FormParam("barCode") String barCode, 
			@FormParam("isbn") String isbn, @FormParam("position") String position, 
			@Context HttpServletRequest request) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		if (Login.isLoginAdmin(idadmin, request)) {
			
			List<Book> books = bookImpl.selectByKey(isbn);
			
			if (books.size() > 0) {
				
				Barcode barcode = new BarcodeImpl().selectByCode(barCode);
				if (barcode != null) {
					
				
				
				BookSingle bookSingle = new BookSingle(barCode, isbn, idadmin, 0, position);
				
				int flag = bookSingleImpl.insertOne(bookSingle);
				
				if (flag == 1) {
					List<BookMessage> bookMessages = bookMessageImpl.selectByKey(isbn);
					
					if (bookMessages.size() > 0) {
						BookMessage bookMessage = bookMessages.get(0);
						
						bookMessage.setIsBespeak(0);
						bookMessage.setTotal(bookMessage.getTotal() + 1);
						bookMessage.setSurplus(bookMessage.getSurplus() + 1);
						bookMessage.setSurplus_show(bookMessage.getSurplus_show() + 1);
						
						int flag_1 = bookMessageImpl.updateOne(bookMessage);
						
						if (flag_1 == 1) {
							globalMap.put(STATUS_CODE, SUCCESS);
						} else {
							globalMap.put(STATUS_CODE, INSERT_WR);
						}
					}
					else {
						BookMessage bookMessage = new BookMessage();
						bookMessage.setIsBespeak(0);
						bookMessage.setIsbn(isbn);
						bookMessage.setNumber(0);
						bookMessage.setSurplus(1);
						bookMessage.setSurplus_show(1);
						bookMessage.setTotal(1);
						
						int flag_1 = bookMessageImpl.insertOne(bookMessage);
						if (flag_1 == 1) {
							globalMap.put(STATUS_CODE, SUCCESS);
						} else {
							globalMap.put(STATUS_CODE, INSERT_WR);
						}
					}
					
					// 把条形码变成使用状态
					if (barcode != null) {
						barcode.setDeleteStatus(1);
						barcodeImpl.updateOne(barcode);
					}
					
				} else {
					globalMap.put(STATUS_CODE, INSERT_WR);
				}
				} else {
					globalMap.put(STATUS_CODE, ERROR);
					globalMap.put("message", "该条形码不存在！");
				}
			} else if (books.size() == 0) {
				globalMap.put(STATUS_CODE, SELECT_EMPTY);
			} else {
				globalMap.put(STATUS_CODE, SELECT_WR);
			}
		}
		else {
			globalMap.put(STATUS_CODE, LOGIN_WR);
		}
		
		return toJsonUtils.map2Json(globalMap);
	}
	
	/**
	 * @Author shisfish
	 * @Date 2016年1月5日 下午4:20:31
	 * @Descrition : 查询所有单本书信息
	 * @return
	 */
	@GET
	@Path("selectAll")
	@Produces("application/json; charset=utf-8")
	public String selectAll() {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		List<BookSingle> bookSingles = bookSingleImpl.selectAll();
		
		if (bookSingles.size() > 0) {
			globalMap.put("list", bookSingles);
		}
		
		return toJsonUtils.map2Json(globalMap);
	}
	
	@POST
	@Path("deleteByBarCode/{idadmin}")
	@Produces("application/json; charset=utf-8")
	public String deleteSingleBook(@PathParam("idadmin") String idadmin, @Context HttpServletRequest request, 
			@FormParam("barCode") String barCode) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		if (Login.isLoginAdmin(idadmin, request)) {
			
			// 找到这本书，若是还处于借书状态，不能更改
			BookSingle bookSingle = bookSingleImpl.selectBarCode(barCode);
			if (bookSingle.getState() == 2) {
				globalMap.put(STATUS_CODE, NOT_LOGGED_IN);
			} else {
				// 找到bookmessage
				BookMessage bookMessage = bookMessageImpl.selectByISBN(bookSingle.getIsbn());
				if (bookMessage != null) {
					bookMessage.setTotal(bookMessage.getTotal() - 1);
					if (bookMessage.getSurplus() > 1) {
						bookMessage.setSurplus(bookMessage.getSurplus() - 1);
					}
					if (bookMessage.getSurplus_show() > 1) {
						bookMessage.setSurplus_show(bookMessage.getSurplus_show() - 1);
					}
					
					bookMessageImpl.updateOne(bookMessage);
					bookSingleImpl.deleteOneByKey(barCode);
					globalMap.put(STATUS_CODE, SUCCESS);
				}
			}
		} else {
			globalMap.put(STATUS_CODE, NOT_LOGGED_IN);
		}
		
		return toJsonUtils.map2Json(globalMap);
	}
	
	/**
	 * @Author shisfish
	 * @Date 2016年1月5日 下午4:21:11
	 * @Descrition : 同过isbn查询单本书的信息
	 * @param isbn
	 * @return
	 */
	@GET
	@Path("selectByISBN/{isbn}")
	@Produces("application/json; charset=utf-8")
	public String selectByISBN(@PathParam("isbn") String isbn) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		
		List<BookSingle> bookSingles = bookSingleImpl.selectByISBN(isbn);
		
		if (bookSingles.size() > 0) {
			globalMap.put("list", bookSingles);
			globalMap.put(STATUS_CODE, SUCCESS);
		} 
		else if (bookSingles.size() == 0) {
			globalMap.put(STATUS_CODE, SELECT_EMPTY);
		}
		else {
			globalMap.put(STATUS_CODE, SELECT_WR);
		}
		
		return toJsonUtils.map2Json(globalMap);
	}
	
	/**
	 * @Author shisfish
	 * @Date 2016年1月5日 下午4:21:47
	 * @Descrition : 查询某天单本书添加的记录
	 * @param choDate
	 * @return
	 */
	@POST
	@Path("selectByDate/{idadmin}/{currentPage}/{pageSize}")
	@Produces("application/json; charset=utf-8")
	public String selectByDate(@PathParam("idadmin") String idadmin, @FormParam("choDate") String choDate, 
			@Context HttpServletRequest request, 
			@PathParam("currentPage") int currentPage, @PathParam("pageSize") int pageSize) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		if (Login.isLoginAdmin(idadmin, request)) {
			
			int count = bookSingleImpl.countByDate(choDate);
			
			if (count > 0) {
				
				Page page = PageUtils.createPage(count, currentPage, pageSize);
				List<Map<String, Object>> bookSingles = bookSingleImpl.selectByDate(choDate, page.getBeginIndex(), pageSize);
				
				if (bookSingles.size() > 0) {
					globalMap.put("list", bookSingles);
					globalMap.put("page", page);
					globalMap.put(STATUS_CODE, SUCCESS);
				} else if (bookSingles.size() == 0) {
					globalMap.put(STATUS_CODE, SELECT_EMPTY);
				} else {
					globalMap.put(STATUS_CODE, SELECT_WR);
				}
			} else if (count == 0) {
				globalMap.put(STATUS_CODE, SELECT_EMPTY);
			} else {
				globalMap.put(STATUS_CODE, SELECT_WR);
			}
		} else {
			globalMap.put(STATUS_CODE, NOT_LOGGED_IN);
		}
		
		return toJsonUtils.map2Json(globalMap);
		
	}
	
	/**
	 * @Author shisfish
	 * @Date 2016年1月5日 下午4:28:22
	 * @Descrition : 查询某天单本书添加的记录（按添加者排序）
	 * @param idadmin
	 * @param choDate
	 * @param request
	 * @return
	 */
	@POST
	@Path("adderLogByDate/{idadmin}/{currentPage}/{pageSize}")
	@Produces("application/json; charset=utf-8")
	public String adderLogByDate(@PathParam("idadmin") String idadmin, @FormParam("choDate") String choDate, 
			@Context HttpServletRequest request, 
			@PathParam("currentPage") int currentPage, @PathParam("pageSize") int pageSize) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		if (Login.isLoginAdmin(idadmin, request)) {
			
			int count = bookSingleImpl.countAdderLogByDate(choDate);
			
			if (count > 0) {
				Page page = PageUtils.createPage(count, currentPage, pageSize);
				List<Map<String, Object>> bookSingles = bookSingleImpl.adderLogByDate(choDate, page.getBeginIndex(), pageSize);
				
				if (bookSingles.size() > 0) {
					globalMap.put("list", bookSingles);
					globalMap.put("page", page);
					globalMap.put(STATUS_CODE, SUCCESS);
				} else if (bookSingles.size() == 0) {
					globalMap.put(STATUS_CODE, SELECT_EMPTY);
				} else {
					globalMap.put(STATUS_CODE, SELECT_WR);
				}
			} else if (count == 0) {
				
			} else {

			}
			
			
		} else {
			globalMap.put(STATUS_CODE, NOT_LOGGED_IN);
		}
		
		return toJsonUtils.map2Json(globalMap);
		
	}
	
	/**
	 * @Author shisfish
	 * @Date 2016年1月5日 下午4:30:00
	 * @Descrition : 通过管理员id查询某天添加单本书的记录（分页）
	 * @param idadmin
	 * @param userId
	 * @param choDate
	 * @param request
	 * @return
	 */
	@POST
	@Path("adderLogByDateAdder/{idadmin}/{currentPage}/{pageSize}")
	@Produces("application/json; charset=utf-8")
	public String adderLogByDateAdder(@PathParam("idadmin") String idadmin, @FormParam("userId") String userId, 
			@FormParam("choDate") String choDate, @Context HttpServletRequest request, 
			@PathParam("currentPage") int currentPage, @PathParam("pageSize") int pageSize) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		
		if (Login.isLoginAdmin(idadmin, request)) {
			
			int count = bookSingleImpl.countAdderLogByDateAdder(userId, choDate);
			
			if (count > 0) {
				Page page = PageUtils.createPage(count, currentPage, pageSize);
				List<Map<String, Object>> bookSingles = bookSingleImpl.adderLogByDateAdder(userId, choDate, page.getBeginIndex(), pageSize);
				if (bookSingles.size() > 0) {
					globalMap.put("list", bookSingles);
					globalMap.put("page", page);
					globalMap.put(STATUS_CODE, SUCCESS);
				} else if (bookSingles.size() == 0) {
					globalMap.put(STATUS_CODE, SELECT_EMPTY);
				} else {
					globalMap.put(STATUS_CODE, SELECT_WR);
				}
			} else if (count == 0) {
				globalMap.put(STATUS_CODE, SELECT_EMPTY);
			} else {
				globalMap.put(STATUS_CODE, SELECT_WR);
			}
			
		} else {
			globalMap.put(STATUS_CODE, NOT_LOGGED_IN);
		}
		
		return toJsonUtils.map2Json(globalMap);
		
	}
	
	/**
	 * @Author shisfish
	 * @Date 2016年1月12日 下午9:50:48
	 * @Descrition : 通过 barCode 查询书的信息
	 * @param barCode
	 * @return
	 */
	@GET
	@Path("selectByBarCode/{barCode}")
	@Produces("application/json; charset=utf-8")
	public String selectByBarCode(@PathParam("barCode") String barCode) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		
		List<Map<String, Object>> books = bookSingleImpl.selectByBarCode(barCode);
		
		if (books.size() > 0) {
			globalMap.put("book", books.get(0));
		} else if (books.size() == 0) {
			globalMap.put(STATUS_CODE, SELECT_EMPTY);
		} else {
			globalMap.put(STATUS_CODE, SELECT_WR);
		}
		
		return toJsonUtils.map2Json(globalMap);
	}
	
	@POST
	@Path("updateASingleBook/{idadmin}")
	@Produces("application/json; charset=utf-8")
	public String updateASingleBook(@PathParam("idadmin") String idadmin, @FormParam("barCode") String barCode, 
			@FormParam("position") String position, @Context HttpServletRequest request) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		
		if (Login.isLoginAdmin(idadmin, request)) {
			
			List<BookSingle> bookSingles = bookSingleImpl.selectByKey(barCode);
			
			if (bookSingles.size() > 0) {
				BookSingle bookSingle = bookSingles.get(0);
				bookSingle.setAdder(idadmin);
				bookSingle.setPosition(position);
				
				int flag = bookSingleImpl.updateOne(bookSingle);
				if (flag == 0) {
					globalMap.put(STATUS_CODE, UPDATE_WR);
				} else {
					globalMap.put(STATUS_CODE, SUCCESS);
				}
			} else {
				globalMap.put(STATUS_CODE, SELECT_EMPTY);
			}
			
		} else {
			globalMap.put(STATUS_CODE, LOGIN_WR);
		}
		
		return toJsonUtils.map2Json(globalMap);
	}
	
	@GET
	@Path("selectNewAddPage/{type}/{currentPage}/{pageSize}")
	@Produces("application/json; charset=utf-8")
	public String selectNewAddPage(@PathParam("type") int type, @PathParam("currentPage") int currentPage, @PathParam("pageSize") int pageSize) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		
		int count = bookSingleImpl.countNewAdd(type);
		
		if (count > 0) {
			Page page = PageUtils.createPage(count, currentPage, pageSize);
			List<Map<String, Object>> bookSingles = bookSingleImpl.selectNewAddPage(type, page.getBeginIndex(), pageSize);
			
			if (bookSingles.size() > 0) {
				globalMap.put(STATUS_CODE, SUCCESS);
				globalMap.put("page", page);
				globalMap.put("list", bookSingles);
			} else if (bookSingles.size() == 0) {
				globalMap.put(STATUS_CODE, SELECT_EMPTY);
			} else {
				globalMap.put(STATUS_CODE, SELECT_WR);
			}
		} else if (count == 0) {
			globalMap.put(STATUS_CODE, SELECT_EMPTY);
		} else {
			globalMap.put(STATUS_CODE, SELECT_WR);
		}

		return toJsonUtils.map2Json(globalMap);
	}
	
}
